package com.util;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author lvcaihong
 * @dtae 2019/2/14 17:32
 */
public class MD5Util {
    public static final String salt = "1a2b3c4d";

    public static String md5(String str){
        return DigestUtils.md5Hex(str);
    }

    /**
     * 将用户输入的密码进行拼装，传到服务器端
     *
     * @param inputPass
     * @return
     */
    public static String inputPassToFormPass(String inputPass){
        String str = salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        System.out.println(str);
        return md5(str);
    }

    /**
     * 密码+盐 存储到数据库
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass,String salt){
        //salt实际中是随机的
        String str = salt.charAt(0) + salt.charAt(2) + formPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    public static String inputPassToDBPass(String input,String saltDB){
        String formPass = inputPassToFormPass(input);
        String dbPass = formPassToDBPass(formPass,saltDB);
        return dbPass;
    }

    public static void main(String[] args) {
//        System.out.println(inputPassToFormPass("123456"));
//        System.out.println(formPassToDBPass(inputPassToFormPass("123456"),"1q2w3e4r"));
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));
    }
}
